<?php

/**
 * @name HookPlugin
 * @desc Yaf定义了如下的6个Hook,插件之间的执行顺序是先进先Call
 * @see http://www.php.net/manual/en/class.yaf-plugin-abstract.php
 * @author luomo-l540\luomo
 */

use Doraemon\tools\HhzClient;
use Doraemon\tools\HhzEnv;
use Doraemon\tools\Log;

class HookPlugin extends Yaf_Plugin_Abstract
{

    public function routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
    {
    }

    public function routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
    {
        $_SERVER['MODULE_NAME'] = $request->getModuleName();

    }

    public function dispatchLoopStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
    {
    }

    public function preDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
    {
    }

    public function postDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
    {
    }

    public function dispatchLoopShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
    {
        if (HhzClient::isLogExpose()) {
            \SeasLog::setBasePath('/data/logs_bak/seaslog');
            \SeasLog::setLogger('exposure');
            \SeasLog::info(json_encode([
                'domain' => $request->getServer()['HTTP_HOST'],
                'url' => $request->getRequestUri(),
                'request_params' => $request->getRequest(),
                'uid' => HhzClient::getUid(),
                'vid' => HhzClient::getVid(),
                'res_json' => $response->getBody()
            ]));
        }

        $env = HhzEnv::getAppEnv();
        $config = Yaf_Registry::get('config');
        if ($config->application->tideways->$env) {
            if (is_dir($config->application->tideways->path) && is_writable($config->application->tideways->path) && function_exists('tideways_xhprof_disable')) {
                list($usec, $sec) = explode(" ", microtime());
                $endTime = (float)$sec + (float)$usec;
                $startTime = Yaf_Registry::get('startTime');
                if (($endTime - $startTime) > $config->application->tideways->runtime) {
                    $environment = getenv('ENV_DOMAIN_SUFFIX');
                    if ($environment === false) {
                        $environment = gethostname();
                        if (strpos($environment, 'gray') !== false) {
                            $environment = substr($environment, strpos($environment, 'gray'), 5);
                        }
                    }
                    $date = date('Y-m-d');
                    $data = tideways_xhprof_disable();
                    $path = $config->application->tideways->path . '/' . $environment . '/' . $date . '/tapi';
                    if (!is_dir($path)) {
                        mkdir($path, 0755, true);
                    }
                    if (is_writable($path)) {
                        file_put_contents($path . '/' . $_SERVER['HTTP_TRACEID'] . '.' . $environment . "-tapi" . str_replace('/', "-", $request->getRequestUri()) . '.xhprof', serialize($data));
                    }
                }
            }
        }

        if (isset($config->application->memoryUsage)) {
            $memoryUsage = memory_get_usage();
            if ($memoryUsage > $config->application->memoryUsage->limit) {
                Log::sys_debug_info($memoryUsage . "#" . $request->getRequestUri());
            }
        }
    }
}
